<?php
class cart
{
	var $db;
	
	function cart()
	{
		global $db;	
		$this -> db     = & $db;
	
	}
	//获取购物车单个商品信息
	function get_cart_detail($id=NULL)
	{
		global $buid;
		$sql="select * from ".CART." where id=$id";
		$this->db->query($sql);
		$re=$this->db->fetchRow();	
		return $re;
	}
	
	//获取一个卖家的已放入购物车的商品信息
	function get_prolist($sell_userid=NULL)
	{
		global $buid;
		//单个卖家的商品总价
		$sumprice=0;
		//单个卖家的平邮总价
		$pu=0;
		//单个卖家的快递总价
		$kd=0;
		//单个卖家的EMS总价
		$ems=0;
		$sql="select a.*,a.price*a.num as sumprice,b.amount,a.price,b.pname,b.pic,b.id as pid,b.freight  from 
		".CART." a left join 
		".PRO." b on  a.pid=b.id  where a.sell_userid=$sell_userid and a.userid=$buid";
		$this->db->query($sql);	 	
		$list['prolist']=$re=$this->db->getRows();	
		
		foreach($re as $key=>$val)
		{   
			$list['prolist'][$key]['pic']=explode(",",$val['pic']);
			//平邮 //快递 //EMS
			$freight=explode(",",$val['freight']);
			$pu+=empty($freight[0])?0:$freight[0]*$val['num'];
			$kd+=empty($freight[1])?0:$freight[1]*$val['num'];
			$ems+=empty($freight[2])?0:$freight[2]*$val['num'];
			$sumprice+=$val['sumprice'];
			unset($freight);
		}
		$list['pu']=$pu;
		$list['kd']=$kd;
		$list['ems']=$ems;
		$list['sumprice']=$sumprice;
		return $list;
	}
	
	//获取购物车卖家列表及卖家商品列表信息及总商品价格
	function get_listcart()
	{
		global $buid;  
		$sumprice=0;			
		$sql="select a.id,a.sell_userid,b.company,b.logo,b.tel  from ".CART." a left join    
		".USER." b on a.sell_userid=b.userid where a.userid=$buid  group by sell_userid";
		
		$this->db->query($sql);
		$re=$this->db->getRows();	
		foreach($re as $key=>$val)
		{	
		     //保存单个商铺商品总价 //平邮 //快点 //EMS 总邮费
			$list[$key]['company']=$val;
			$pro=$this->get_prolist($val['sell_userid']);			
			$list[$key]['company']['sumprice']=$pro['sumprice'];
			$list[$key]['company']['pu']=$pro['pu'];
			$list[$key]['company']['kd']=$pro['kd'];
			$list[$key]['company']['ems']=$pro['ems'];	
			$sumprice+=$pro['sumprice'];
			//保存商铺的所有商品信息
			$list[$key]['prolist']=$pro['prolist'];
			unset($pro);
		}
		$list['sumprice']=$sumprice;	
		return $list;
	}
	//增加商品
	function add_cart($prid=NULL,$num=1)
	{
		global $buid;  
		$sql="select userid,price from ".PRO." where id=$prid";
		$this->db->query($sql);
		$pro=$this->db->fetchRow(); 
		if(empty($pro['userid']))
		{   
			return 'error';
		}
		
		$sql="select a.id,b.amount,a.num from ".CART." a left join 
		".PRO." b on  a.pid=b.id   where pid=$prid and a.userid=$buid limit 1";
		$this->db->query($sql);
		$re=$this->db->fetchRow();	
		if(!empty($re['num'])&&($re['num']+$num)>$re['amount'])
		{//库存不够	
			$sql="update ".CART." set num=$re[amount] where id=$re[id]";	
			$this->db->query($sql);			
			return 'error1';
		}		
		if(!empty($re['id']))
			return false;	//如果商品已经存在		   
		$sql="insert into ".CART."(`userid` ,`pid` ,`sell_userid` ,`price` ,`num` ,`time` )
		VALUES ('$buid', '$prid', '$pro[userid]', '$pro[price]',$num, ".time().")"; 	  
		$this->db->query($sql);	   
	}
	//删除购物车内容
	function del_cart($id=NULL)
	{
		$id*=1;
		$sql="delete from ".CART." where id='$id'";
		$flag=$this->db->query($sql);	   
		return $flag;
	}
	//清空购物车内容
	function clear_cart(){
		global $buid;  
		$sql="delete from ".CART." where userid='$buid'";
		$flag=$this->db->query($sql);	   
		return $flag;
	}
	
	//编辑购物车数量
	function edit_cart($cartid=NULL,$num=NULL)
	{
		global $buid;  
		if($num<1&&!empty($cartid))//如果数量小于1就删除
			$this->del_cart($cartid);
		$sql="select b.amount,a.num from ".CART." a left join 
			".PRO." b on  a.pid=b.id   where a.id=$cartid and a.userid=$buid limit 1";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		if($num>$re['amount']&&!empty($re['amount']))
		{
			$sql="update ".CART." set num='$re[amount]' where id=$cartid";	
			$this->db->query($sql);			
			return 'error';//库存不够	
		}
		else
		{  
			$sql="update ".CART." set num='$num' where id='$cartid'";
			$flag=$this->db->query($sql);	   
			return false;
		}
	}

}
?>
